package com.moss.cloud.base.biz.controller;

import com.moss.cloud.base.api.model.from.BaseFrom;
import com.moss.cloud.base.api.model.from.ConfigFrom;
import com.moss.cloud.base.api.model.from.SystemConfigFrom;
import com.moss.cloud.base.api.model.param.ConfigParam;
import com.moss.cloud.base.api.model.param.SystemConfigParam;
import com.moss.cloud.base.biz.service.ISystemConfigService;
import com.moss.cloud.common.core.constant.ServiceList;
import com.moss.cloud.common.core.model.Result;
import com.moss.cloud.common.security.annotation.SysLog;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 系统参数配置前段控制器
 *
 * @author 瑾年
 * @date 2023年10月19日
 */
@RestController
@RequestMapping(ServiceList.BASE + "/systemConfig")
public class SystemConfigController {
    private ISystemConfigService systemConfigService;

    public SystemConfigController(ISystemConfigService systemConfigService) {
        this.systemConfigService = systemConfigService;
    }

    /**
     * 系统参数列表
     *
     * @param systemConfigParam
     * @return
     */
    @PostMapping("/getConfigs")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "系统参数列表")
    public Result<?> getConfigs(@RequestBody SystemConfigParam systemConfigParam) {
        return Result.success(systemConfigService.getConfigs(systemConfigParam));
    }

    /**
     * 新增或修改配置
     *
     * @param systemConfigFrom
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "新增或修改配置")
    public Result<?> saveOrUpdate(@RequestBody @Validated SystemConfigFrom systemConfigFrom) {
        return Result.success(systemConfigService.saveOrUpdate(systemConfigFrom));
    }

    /**
     * 删除系统参数配置
     *
     * @param baseFrom
     * @return
     */
    @PostMapping("/delete")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "删除系统参数配置")
    public Result<?> delete(@RequestBody @Validated BaseFrom baseFrom) {
        return Result.success(systemConfigService.delete(baseFrom));
    }

    /**
     * 刷新配置缓存
     *
     * @return
     */
    @PostMapping("/loadSystemConfig")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "刷新配置缓存")
    public Result<?> loadSystemConfig() {
        systemConfigService.loadSystemConfig();
        return Result.success();
    }

    /**
     * 更新配置值
     *
     * @param configFrom
     * @return
     */
    @PostMapping("/updateValue")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "更新配置值")
    public Result<?> updateValue(@RequestBody @Validated ConfigFrom configFrom){
        return Result.success(systemConfigService.updateValue(configFrom));
    }


    /**
     * 查看密码
     *
     * @param configParam
     * @return
     */
    @PostMapping("/showPassword")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "查看密码")
    public Result<?> showPassword(@RequestBody @Validated ConfigParam configParam){
        return Result.success(systemConfigService.showPassword(configParam));
    }

}
